package com.test.repository;

import com.test.entity.Employee;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.lang.annotation.Native;
import java.util.List;


/**
 * Created by zhang on 2017/6/7.
 */
public interface EmployeeRepository extends Repository<Employee,Integer> {

    public Employee findByName(String name);

    public Employee findById(Integer id);

    public List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);

    public List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age);

    public List<Employee> findByNameStartingWithOrAgeLessThan(String name, Integer age);

    public List<Employee> findByNameInOrAgeLessThan(List<String> names,Integer age);

    @Query("select o from Employee o where id = (select max(id) from Employee)")
    public Employee getEmployeeByMaxId();

    @Query("select o from Employee o where o.name = ?1 and o.age = ?2")
    public List<Employee> queryParams1(String name,Integer age);

    @Query("select o from Employee o where o.name = :name and o.age = :age")
    public List<Employee> queryParams2(@Param("name")String name, @Param("age") Integer age);

    @Query(nativeQuery = true,value = "select o.* from employee o where o.name = :name and o.age = :age")
    public List<Employee> queryParams3(@Param("name")String name, @Param("age") Integer age);

    @Modifying
    @Query("update Employee o set o.age = :age where o.id = :id")
    public void update(@Param("id")Integer id, @Param("age") Integer age);
}
